<?php
namespace common\modules\user\frontend;

use yii;
use yii\base\InvalidConfigException;

class Module extends \common\core\FrontendModule
{

    /**
     *
     * @var string Alias for module
     */
    public $alias = "@userFrontend";

    /**
     *
     * @var bool If true, users are required to enter an email
     */
    public $requireEmail = true;

    /**
     *
     * @var bool If true, users are required to enter a username
     */
    public $requireUsername = false;

    /**
     *
     * @var bool If true, users can enter an email. This is automatically set to true if $requireEmail = true
     */
    public $useEmail = true;

    /**
     *
     * @var bool If true, users can enter a username. This is automatically set to true if $requireUsername = true
     */
    public $useUsername = true;

    /**
     *
     * @var bool If true, users can log in using their email
     */
    public $loginEmail = true;

    /**
     *
     * @var bool If true, users can log in using their username
     */
    public $loginUsername = true;

    /**
     *
     * @var int Login duration
     */
    public $loginDuration = 2592000;
 // 1 month
    /**
     *
     * @var array|string|null Url to redirect to after logging in. If null, will redirect to home page. Note that
     *      AccessControl takes precedence over this (see [[yii\web\User::loginRequired()]])
     */
    public $loginRedirect = null;

    /**
     *
     * @var array|string|null Url to redirect to after logging out. If null, will redirect to home page
     */
    public $logoutRedirect = null;

    /**
     *
     * @var bool If true, users will have to confirm their email address after registering (= email activation)
     */
    public $emailConfirmation = true;

    /**
     *
     * @var bool If true, users will have to confirm their email address after changing it on the account page
     */
    public $emailChangeConfirmation = true;

    /**
     *
     * @var string Time before userKeys expire (currently only used for password resets)
     */
    public $resetKeyExpiration = "48 hours";

    /**
     *
     * @var string Email view path
     */
    public $emailViewPath = "common/modules/user/frontend/mail";

    public $controllerNamespace = 'common\modules\user\frontend\controllers';

    public function init()
    {
        parent::init();
        
        // check for valid email/username properties
        $this->checkModuleProperties();
    }

    /**
     * Check for valid email/username properties
     */
    protected function checkModuleProperties()
    {
        // set use fields based on required fields
        if ($this->requireEmail) {
            $this->useEmail = true;
        }
        if ($this->requireUsername) {
            $this->useUsername = true;
        }
        // get class name for error messages
        $className = get_called_class();
        // check required fields
        if (! $this->requireEmail && ! $this->requireUsername) {
            throw new InvalidConfigException("{$className}: \$requireEmail and/or \$requireUsername must be true");
        }
        // check login fields
        if (! $this->loginEmail && ! $this->loginUsername) {
            throw new InvalidConfigException("{$className}: \$loginEmail and/or \$loginUsername must be true");
        }
        // check email fields with emailConfirmation/emailChangeConfirmation is true
        if (! $this->useEmail && ($this->emailConfirmation || $this->emailChangeConfirmation)) {
            $msg = "{$className}: \$useEmail must be true if \$email(Change)Confirmation is true";
            throw new InvalidConfigException($msg);
        }
        // ensure that the "user" component is set properly
        // this typically causes problems in the yii2-advanced app
        // when people set it in "common/config" instead of "frontend/config" and/or "backend/config"
        // -> this results in users failing to login without any feedback/error message
        if (! Yii::$app->request->isConsoleRequest && ! Yii::$app->get("user")) {
            throw new InvalidConfigException('Yii::$app->user is not set properly.');
        }
    }
}
